home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1017 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  74 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: johnw@jove.acs.unt.edu (John R. Williams)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: C++ syntactic trap
  5. Date: 09 Apr 1996 09:59:37 PDT
  6. Organization: University of North Texas
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4kcrhk$aj1@hermes.acs.unt.edu>
  9. References: <4k3q4p$lkd@syn.cs.cornell.edu>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 9 Apr 1996 05:11:16 GMT
  12. X-Newsreader: TIN [version 1.2 PL2]
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMWqXeky4NqrwXLNJAQGQxQIAj6vfvv0PB/JKV0dnDHtzFdRYZUQwF6dP
  15.     8QfGUlZ5orSAVnUhHp6F45K9YhqzAiLyUc/+DQucugBNXehQwAxK9A==
  16.     =/YFy
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. Stephen Vavasis (vavasis@CS.Cornell.EDU) wrote:
  20. > #include <iostream.h>
  21. > int main() {
  22. >   cout << " How many *'s? ";
  23. >   int sz; cin >> sz;
  24. >   char* a = new char(sz + 1); // bug is here, but syntax is legal.
  25. >   for (int i = 0; i < sz; i++)
  26. >     a[i] = '*';
  27. >   a[sz] = 0;
  28. >   cout << a << endl;
  29. >   delete[] a;
  30. >   return 0;
  31. > }
  32.  
  33. > (Does everyone see the error?  I did not, even after staring at my
  34. > code for a long time.  The point is that the marked statement is an
  35. > unintended cast from int to char because I used () instead of [].)
  36.  
  37. > I would like to make a plea to the compiler-writers who read this
  38. > group: please issue warnings for syntactic trouble spots!  Implicit
  39. > type conversion probably creates other traps that I haven't thought
  40. > of.  C++ programmers like me need help from the compiler to navigate
  41. > the traps!
  42.  
  43. At the risk of sounding insulting, I wonder if you have been using C++ 
  44. long. This strikes being similar the the error in the latest 
  45. PC-Lint ads ("for (i = 0; i <= 10; i++)"): It looks like it would be easy 
  46. to make but does not occur often in practice because programmers who use 
  47. the language often are simply not accustomed to writing the incorrect 
  48. version and would never do it under normal circumstances.
  49.  
  50. I agree that it is uusual to allocate a single object of a built-in type 
  51. this way, but how do you propose to write this in such a way that such a 
  52. warning would not be generated? All I can think of is to warn when *any* 
  53. conversion occurs (this one looks pretty explicit to me), forcing you to 
  54. write something like this:
  55.  
  56.   char *foo = new char(static_cast<char>(10));
  57.  
  58. (Actually this rule doesn't seem too bad if applied only to built-in 
  59. types...)
  60.  
  61. --
  62. class JohnWilliams: public Student, public Programmer {
  63. public:
  64.   char const *operator&() const { return "johnw@jove.acs.unt.edu"; }
  65.   char const *homepage () const { return "http://www.unt.edu/~johnw"; }
  66. };
  67. ---
  68. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  69.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  70.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  71.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  72.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  73. ]
  74.